package cn.lhl.web.controller;


import cn.lhl.domain.system.Module;
import cn.lhl.domain.system.User;
import cn.lhl.service.system.ModuleService;
import cn.lhl.service.system.UserService;
import com.github.pagehelper.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private ModuleService moduleService;

    @RequestMapping("/login")
    public String login(String email, String password) {
        //若果传入的参数email和password都为空,停留在login.jsp界面,且显示提示信息
        if (StringUtil.isEmpty(email) || StringUtil.isEmpty(password)) {
            return "forward:login.jsp";
        }
       /* //查询是否有该用户名
        User user = userService.findByEmail(email);
        if (user != null) {
            //将输入的密码和数据库中的做对比
            if (password.equals(user.getPassword())) {
                //验证成功将数据写入session
                session.setAttribute("loginUser", user);
                //将模块信息放入session中
                List<Module> module = moduleService.findByUser(user);
                session.setAttribute("modules", module);
                return "home/main";
            } else {
                request.setAttribute("error", "密码有误");
                return "forward:login.jsp";
            }

        } else {
            request.setAttribute("error", "用户名输入有误！");
            return "forward:login.jsp";
        }*/

        try {
            //获取subject对象
            Subject subject = SecurityUtils.getSubject();
            //获取参数构造Token对象
            UsernamePasswordToken upToken = new UsernamePasswordToken(email, password);
            //此步骤将调用自定义的shiro域方法
            subject.login(upToken);
            User user = (User) subject.getPrincipal();

            //判断user是否为null
            if (user != null) {
                session.setAttribute("loginUser", user);
                //获得模块信息将模块信息封装在域对象中,动态分配菜单
                List<Module> modules = moduleService.findByUser(user);
                session.setAttribute("modules", modules);
                return "home/main";
            } else {
                request.setAttribute("error", "用户名输入有误！");
                return "forward:login.jsp";
            }
        } catch (Exception e) {
            //System.out.println(e.getMessage());
            request.setAttribute("error", "密码有误！");
        }
            return "forward:login.jsp";

    }


    /*//退出
    @RequestMapping(value = "/logout", name = "用户登出")
    public String logout() {
        //SecurityUtils.getSubject().logout();   //登出
        return "forward:login.jsp";
    }*/

    //退出
    @RequestMapping(value = "/logout", name = "用户登出")
    public String logout() {
        SecurityUtils.getSubject().logout();   //登出
        return "forward:login.jsp";
    }

    @RequestMapping("/home")
    public String home() {
        return "home/home";
    }
}
